import flash.events.AsyncErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.net.NetConnection;
class Main extends flash.display.MovieClip
{
	static var app:String="/minimal/nc";
	private var nc:NetConnection;
	public static function main()
	{
		flash.Lib.current.addChild(new Main());
	}
	public function new()
	{
		super();
		
		nc = new NetConnection();
		nc.client=this;
		nc.objectEncoding = 0;
		nc.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
		nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onError);
		
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	function onAddedToStage(event:Event)
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		var uri = getUri();
		trace("connecting to rtmp://" + uri);
		nc.connect("rtmp://"+uri);
	}
	function onStatus(event:NetStatusEvent):Void
	{
		trace("onNetStatus code="+event.info.code+", description=" + event.info.description);
	}
	function onError(event:AsyncErrorEvent):Void
	{
		trace("onError currentTarget="+event.currentTarget+", error=" + event.error);
	}
	function getUri():String
	{
		var parameters:Dynamic = flash.Lib.current.loaderInfo.parameters;
		trace(parameters);
		var u = Reflect.field(parameters, "uri");
		if(u!=null)
			return u;
		var h = Reflect.field(parameters, "host");
		var p = Reflect.field(parameters, "port");
		var a = Reflect.field(parameters, "app");
		return (h==null?"localhost":h) + (p==null?":1935":":"+p) + (a==null?app:a);
	}
}
